<?php
    session_start();
    include '../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    $perti = $_REQUEST['perti'];
    $prioridad = $_REQUEST['prioridad'];
    $fecum2 = $_REQUEST['fecum'];
    $fecum = date('Y-m-d',strtotime($fecum2));
    $dias = $_REQUEST['dias'];
    
    $oids = $_REQUEST['oids'];
    $observ = $_REQUEST['observ'];
    $usuasig = $_REQUEST['usuasig'];
    $estado = $_REQUEST['estado'];
    $ruta = $_REQUEST['ruta'];
    
     $count = "select COUNT(*) from dnconducta where sol_oid =".$oids;
    $res = $con->query($count);
    
    $res2 = mssql_result($res, 0, 0);
    
    if($res2 != 1){
        $his = "insert into dnhistorial(dnsolicitud,hisfecmod,hisestado,usu_oid,hisobs,hisadjuntos)values(".$oids.",GETDATE(),2,".$usuasig.",'','".$ruta."')";
        $con->query($his);

        $edi = "update dnhistorial
                    set hisobs = '".$observ."'
                    where dnsolicitud =".$oids." and hisestado =".$estado;
        $con->query($edi);

        $edi = "update dnsolicitud
                    set dnsestado = '2'
                    where dnsoid =".$oids;
        $con->query($edi);

        $conduc = "insert into dnconducta(conpertinen,conpriorid,confecump,sol_oid,condias,usuasig,usuasig2)values('".$perti."','".$prioridad."','".$fecum."','".$oids."','".$dias."','".$_SESSION['usuario']."','".$usuasig."')"; 
        $con->query($conduc);
    }else{
        
        $edi = "update dnhistorial 
                set hisobs='".$observ."',
                where hisoid=(select top 1 hisoid
                from dnhistorial 
                where dnsolicitud = ".$oids."
                order by 1 desc)";
        $con->query($edi);
        
        $his = "insert into dnhistorial(dnsolicitud,hisfecmod,hisestado,usu_oid,hisobs,hisadjuntos)values(".$oids.",GETDATE(),2,".$usuasig.",'','')";
        $con->query($his);
        $edi = "update dnsolicitud
                    set dnsestado = '2'
                    where dnsoid =".$oids;
        $con->query($edi);
    }
    
    
    $con->cerrarConexion();
    
?>
